'use strict';

var util = require('util');
var yeoman = require('yeoman-generator');
var path = require('path');
var fs = require('fs');

var Generator = module.exports = function Generator(args, options){
    yeoman.generators.Base.apply(this, arguments);
    this.argument('controller_name', {type:String, required: false});
    var utils_path = this.templatePath('../../../utils.js');
    this.utils = require(utils_path);
};

util.inherits(Generator, yeoman.generators.Base);

Generator.prototype.askFor = function askFor(){
    if(this.controller_name)
        return;
    var cb = this.async();
    this.prompt({
        type: 'input',
        name: 'controller',
        message: '请输入控制器的名字',
        default: 'BlogCtrl'
    },function(answer){
        //判断控制器是否已经存在了
        if(fs.existsSync('js/controllers/' + answer.controller+'.js')){
            this.log(util.format('控制器: %s 已经存在了,请重新输入', answer.controller));
            process.exit(1);
        }else{
            this.controller_name = answer.controller;

        }
        cb();
    }.bind(this));
};

Generator.prototype.install = function files(){
    this.utils.addController(this, this.controller_name);
};